#include <myheader.h>

void deep_search(char* root){
    //打开目录流
    DIR* dir_stream = opendir(root);
    ERROR_CHECK(dir_stream,NULL,"DIROPEN");
    struct dirent *dir_struct_p;
    while((dir_struct_p = readdir(dir_stream))!=NULL){
        //跳过. 和.. 不然陷入无限递归
        if(strcmp(dir_struct_p->d_name, ".") == 0 || 
           strcmp(dir_struct_p->d_name,".." )== 0){
            continue;
        }
        //遍历目录流，如果遇到目录，就递归
        char* dir_name = dir_struct_p->d_name;
        if(dir_struct_p->d_type == DT_DIR){
            //获取该目录的名称
            printf("%s\n",dir_name);
            //构建递归目录的绝对路径
            char path_absulote[1024] = {0};
            sprintf(path_absulote,"%s%s%s",root,"/",dir_name);
            deep_search(path_absulote);
        }else{
            //遇到的不是目录
            printf("%s\n",dir_name);
        }
    }
    closedir(dir_stream);
    return;
}
/*Usage:实现简陋tree功能 参数：path */
int main(int argc, char *argv[])
{
    char *path = NULL;
    if(argc == 1){
        path = ".";
    }else if(argc == 2){
        path = argv[1];
    }else{
        fprintf(stderr,"argc num error!\n");
    }
    //先打印根目录
    printf("%s\n",path);
    deep_search(path);
    return 0;
}

